<?php

namespace App\Http\Controllers;

use App\Models\Topic;
use App\Http\Requests\StoreTopicRequest;
use App\Http\Requests\UpdateTopicRequest;
use App\utils\ImageUploader;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class TopicController extends Controller
{

    public function __contruct(){

        $this->middleware(['auth'])->except('index','show');

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $categories = get_categoires_all();
        $topics = Topic::with('user','category')
            ->topicSelect($request->input('order'))
            ->paginate(10);

//        $topics = [];
        return view('topics.index',compact('topics','categories'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        return view('topics.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StoreTopicRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreTopicRequest $request, Topic $topic)
    {

        $topic->title = $request->input('title');
        $topic->content = $request->input('content');
        $topic->category_id = $request->input('category_id');
        $topic->user_id = Auth::id();
        $topic->save();

        return redirect()->to($topic->link())
            ->with('success','帖子发布成功');

    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Topic  $topic
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request,Topic $topic)
    {
        //补全slug，强制301永久重定向
        if(!empty($request->slug) && $request->slug != $topic->slug){
            return redirect()->to($topic->link(),301);
        }

        return view('topics.show',compact('topic'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Topic  $topic
     * @return \Illuminate\Http\Response
     */
    public function edit(Topic $topic)
    {
        //授权策略
        $this->authorize('update',$topic);

        return view('topics.edit',compact('topic'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\UpdateTopicRequest  $request
     * @param  \App\Models\Topic  $topic
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateTopicRequest $request, Topic $topic)
    {
        $this->authorize('update',$topic);

        $topic->update($request->all());

        return redirect()->to($topic->link())
            ->with('success','修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Topic  $topic
     * @return \Illuminate\Http\Response
     */
    public function destroy(Topic $topic)
    {
        $this->authorize('destroy',$topic);

        $category_id = $topic->category_id;
        $topic->delete();
        //todo:回复没删、收藏没删、点赞没删，应该为软删除，这样会有记录存在

        //跳转到对应目录
        return redirect()->route('categories.show',$category_id)
            ->with('success','删除成功');

    }


    /*
     * 富文本图片上传
     * */
    public function imageUpload(Request $request, ImageUploader $uploader){

        $data = [
            'success'=> false,
            'msg'    => "error message",
            'file_path'=> null,
        ];

        if(isset($request->upload_file)){
            $avatar = $uploader->save($request->upload_file,'topics',Auth::id(),720);
            if($avatar){
                $data['success'] = true;
                $data['msg'] = 'upload success';
                $data['file_path'] = $avatar['path'];
            }
        }

        return $data;
    }
}
